//
//  LEDefaultHintView.m
//  qqqqqq
//
//  Created by iOSer on 2017/3/21.
//  Copyright © 2017年 iOSer. All rights reserved.
//

#import "LEDefaultHintView.h"
@interface LEDefaultHintView()
@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) UILabel *titleLab;
@property(nonatomic,strong) UILabel *textLab;
@property(nonatomic,strong) UIButton *okBtn;
@property(nonatomic,strong) UIButton *cancelBtn;
@end

@implementation LEDefaultHintView

-(instancetype)init{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.hidden = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)];
        [self addGestureRecognizer:tap];
        
        [self addSubview:self.bgView];
        [self.bgView addSubview:self.titleLab];
        [self.bgView addSubview:self.textLab];
        [self.bgView addSubview:self.okBtn];
        [self.bgView addSubview:self.cancelBtn];
        self.titleLab.hidden = YES;
        self.okBtn.hidden = YES;
        self.textLab.hidden = YES;
        self.cancelBtn.hidden = YES;
        
        [_bgView makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.equalTo(CGSizeMake(0, 0));
        }];
        [_titleLab makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_bgView).offset(GGRealValue(5));
            make.centerX.equalTo(_bgView);
            make.height.equalTo(@(GGRealValue(15)));
            //            make.size.equalTo(CGSizeMake(GGRealValue(60), GGRealValue(25)));
        }];
        [_textLab makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLab.mas_bottom).offset(GGRealValue(5));
            make.left.equalTo(_bgView).offset(GGRealValue(14));
            make.right.equalTo(_bgView).offset(-GGRealValue(14));
        }];
        [_okBtn makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_bgView).offset(-GGRealValue(10));
            make.left.equalTo(_bgView).offset(GGRealValue(20));
            make.size.equalTo(CGSizeMake(GGRealValue(50), GGRealValue(28)));
        }];
        
        [_cancelBtn makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_bgView).offset(-GGRealValue(10));
            make.right.equalTo(_bgView).offset(-GGRealValue(20));
            make.size.equalTo(CGSizeMake(GGRealValue(50), GGRealValue(28)));
        }];
        
    }
    
    return self;
}



-(void)showLEDefaultHintViewWithTitle:(NSString *)title Text:(NSString *)text{
    
    _titleLab.text = title;
    _textLab.text = text;
    CGFloat contentW = GGRealValue(230)-(GGRealValue(14)*2);
    CGRect tmpRect = [text boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:GGRealValue(12)]} context:nil];
    CGFloat contentH = tmpRect.size.height;
    self.hidden = NO;
    [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.48 initialSpringVelocity:0.6 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        [self.bgView remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.equalTo(CGSizeMake(GGRealValue(230), GGRealValue(68)+contentH));
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.titleLab.hidden = NO;
        self.textLab.hidden = NO;
        self.okBtn.hidden = NO;
        self.cancelBtn.hidden = NO;
    }];
}

-(void)dismissView{
    [self.superview layoutIfNeeded];
    [UIView animateWithDuration:0.3 animations:^{
        self.titleLab.hidden = YES;
        self.textLab.hidden = YES;
        self.okBtn.hidden = YES;
        self.cancelBtn.hidden = YES;
        [self.bgView remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.equalTo(CGSizeZero);
        }];
        [self.superview layoutIfNeeded];//强制绘制
    }completion:^(BOOL finished) {
        self.hidden = YES;
        self.disBlock();
    }];
}

-(void)okBtnClick{
    [self.delegate leDefaultHintViewOkBtnClickWithHintView:self];
    [self dismissView];
}
-(void)cancelBtnClick{
    [self dismissView];
}
-(UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        [_bgView setBackgroundColor:HEXColor(0xffffff, 1)];
        _bgView.layer.borderColor = HEXColor(0x000000, 1).CGColor;
        _bgView.layer.borderWidth = 1;
        _bgView.layer.cornerRadius = 8;
    }
    return _bgView;
}
-(UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithTitle:@"小道消息" color:HEXColor(0x000000, 1) sizeFont:GGRealValue(15)];
        _titleLab.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLab;
}
-(UILabel *)textLab{
    if (!_textLab) {
        _textLab = [[UILabel alloc] initWithTitle:@"购买" color:HEXColor(0x000000, 1) sizeFont:GGRealValue(12)];
        _textLab.textAlignment = NSTextAlignmentCenter;
        _textLab.numberOfLines = 0;
    }
    return _textLab;
}
-(UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton new];
        [_cancelBtn setBackgroundColor:HEXColor(0x000000, 1)];
        [_cancelBtn setTitleColor:HEXColor(0xffffff, 1) forState:UIControlStateNormal];
        [_cancelBtn setTitle:@"算了" forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}
-(UIButton *)okBtn{
    if (!_okBtn) {
        _okBtn = [UIButton new];
        [_okBtn setBackgroundColor:HEXColor(0x000000, 1)];
        [_okBtn setTitleColor:HEXColor(0xffffff, 1) forState:UIControlStateNormal];
        [_okBtn setTitle:@"知道了" forState:UIControlStateNormal];
        [_okBtn addTarget:self action:@selector(okBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _okBtn;
}

@end
